package com.atguigu.gulimall.auth.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.gulimall.common.vo.GiteeUser;
import com.atguigu.gulimall.common.vo.SocialUser;
import com.atguigu.gulimall.common.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoService {

    /**
     * 拿到token之后 获取全部用户信息封装
     */
    public SocialUser getUserInfo(SocialUser user) throws Exception {

        GiteeUser socialUser = null;
        String access_token = user.getAccess_token();
        Map<String, String> query = new HashMap<>();
        query.put("access_token", access_token);
        //https://gitee.com/api/v5/user?access_token=d183fa2da8d988c3a1ab721cfd538165
        HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get",  new HashMap<String, String>(),query );
        if (response.getStatusLine().getStatusCode() == 200) {
            String json = EntityUtils.toString(response.getEntity());
            socialUser = JSON.parseObject(json, new TypeReference<GiteeUser>() {
            });
            user.setUid(String.valueOf(socialUser.getId()));
        }
        return user;

    }
}

